/*--------------------环境模块.设备管理--ly-----------------*/
/*--------------------设备设置规则 en_equipment_setup.html------------------*/

var tableurl = environmentport + "/RuleController/getEnvironmentRule"; //获取全部选项数据
var Jurisdictionurl = environmentport + "/DeviceController/modifyByDeviceid"; //根据id修改规则
var ckeckedurl = environmentport + "/DeviceController/selectByPrimaryKey";//根据id查询

//页面table渲染
var tableget = (function() {
	$.get(tableurl, {
			'page': 1,
			'rows': 100,
		},
		function(result) {
			var code = result.code;
			var list = result.object;
			if(code == 0) {
				var ant = list.total;
				antrows = ant; //将查询结果总数变成全局变量
				$('.totalNum').text(ant);
				$.each(list.list, function(idx, obj) {
					var idx = idx + 1; //排序默认从0开始，idx将作为显示题号，因此+1
					var id = obj.ruleid;
					var Qhtml ='<tr id="' + obj.ruleid + '">\
          <td>\
            <div class="layui-unselect layui-form-checkbox ' + obj.ruleid + '" lay-skin="primary" data-id="' + obj.ruleid + '">\
              <i class="layui-icon">&#xe605;</i>\
            </div>\
          </td>\
          <td>' + idx + '</td>\
          <td>' + obj.rulename + '</td>\
          <td>' + obj.devicetype + '</td>\
        </tr>';
					$("#setup").append(Qhtml);
					var addckecked = (function(id) {
						//获取cookie:rolerid & Wtid
						var deviceid = $.cookie("deviceid");
						$.get(ckeckedurl, {
							deviceid: "8619a2f2-2fbb-4511-8ae8-e28a77528a04"
						}, function(result) {
							console.log(result);
							var code = result.code;
							var jurisdiction = result.object;
							if(code == 0) {							
									var a = jurisdiction.ruleid;
									if(id == a) {
										$("." + id).addClass("layui-form-checked");
									}
							}
						})
					});
					addckecked(id);
				});
			} else {
				alert('您的登录认证已失效，请重新登录！');
			};
			tableCheck.init();
		}, 'JSON');
});
tableget();

//设置规则
function setupAll(argument) {
	var deviceid = $.cookie("deviceid");
	var editcookie = $.cookie("nav");
	var Wtid = JSON.parse(editcookie).w;

	var data = tableCheck.getData();
	var value = JSON.stringify(data);
	value = value.replace('[', '');
	value = value.replace(']', '');
	value = value.replace(/\"/g, ''); //去掉字符串中的英文引号
	layer.confirm('确定为设备设置爱此规则?', function(index) {
		$.get(Jurisdictionurl, {
			'deviceid': deviceid,
			'ruleid': value,
		}, function(result) {
			console.log(value, data);
			var code = result.code;
			if(code == 0) {
				layer.msg('授权成功!', {
					icon: 1,
					time: 1000
				}, function() {
					//关闭当前frame
					parent.layer.close(index);
				});
			} else {
				layer.msg('授权失败，请稍后再试!', {
					icon: 2,
					time: 1000
				}, function() {
					//关闭当前frame
					//parent.layer.close(index);
				});
			}
		})
	});
}